package com.hamizhong.iflowers.web.action.admin.user;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hamizhong.iflowers.service.IUserService;
import com.hamizhong.iflowers.utils.MyConstants;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.utils.MyTools;
import com.hamizhong.iflowers.web.base.MyBaseAjaxAction;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.UserVO;

public class AjaxQueryUsersByConditionAction extends MyBaseAjaxAction {
	
	private IUserService iUserService;
	
	public IUserService getiUserService() {
		return iUserService;
	}
	public void setiUserService(IUserService iUserService) {
		this.iUserService = iUserService;
	}
	
	@Override
	protected JSONObject doRequest() {
		logger.info("...invoke...AjaxQueryUsersByConditionAction...");
		String pageNum = getRQ().getParameter("pageNum");
		String searchCondition = getRQ().getParameter("searchCondition");

		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		JSONObject jobj = null;
		
		IOPResultDto<IObjectVO> resultDto = iUserService.findByCondition(searchCondition, Integer.valueOf(pageNum), MyConstants.PAGE_SIZE_COMMON_ADMIN);
		if(!resultDto.isSuccess()) {
			jo.put("result", MyConstants.AJAX_FAILED_ALERT);
			jo.put("msg", MyErrorMsgConstants.FAILED_TO_QUERY_USER_BY_CONDITION_NULL);
			return jo;
		}
		
		IObjectVO iObjectVO = resultDto.getFirstResult();
		List<UserVO> userVOList = iObjectVO.getUserVOList();
		int totalUserCnt = iObjectVO.getTotalUserCnt();
		
		//设置总条数
		jo.put("totalCnt", totalUserCnt+"");
		//设置当前页
		jo.put("pageNum", pageNum);
		//设置总页数
		jo.put("totalPageCnt", MyTools.convertItemCnt2PageCnt(totalUserCnt, MyConstants.PAGE_SIZE_COMMON_ADMIN)+"");
		for (UserVO userVO : userVOList) {
			jobj = new JSONObject();
			jobj.put("userId", userVO.getUserId());
			jobj.put("userName", userVO.getUserName());
			jobj.put("userEmail", userVO.getUserEmail());
			jobj.put("userPhone", userVO.getUserPhone());
			jobj.put("createDate", MyTools.formatDttm2YYYY_MM_DD(userVO.getCreateDate()));
			ja.add(jobj);
		}
		jo.put("data", ja.toString());
		jo.put("result", MyConstants.AJAX_SUCCESS);
		return jo;
	}

}
